package com.czy.owner.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.entity.GetCommentModel;
import com.czy.owner.entity.LogisticsModel;
import com.czy.owner.entity.UserLocationModel;
import com.czy.owner.utils.glide.GlideUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import okhttp3.internal.http.HttpDate;


/**
 * Created by wangyang on 2017/5/18 0018.
 */

public class LogisticsAdapter extends RecyclerArrayAdapter<LogisticsModel> {
    private Context context;
    public LogisticsAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new LogisticsViewHolder(parent);
    }

    class LogisticsViewHolder extends BaseViewHolder<LogisticsModel> {
        private EasyRecyclerView rvImg;
        private TextView tvLogisticsStatus;
        private TextView tvLogisticsShippername;
        private TextView tvLogisticsCode;
        private EasyRecyclerView easyRvSubLogistics;
        private LogisticsSubAdapter logisticsSubAdapter;
        private SeeEvaluateImgAdapter adapter;
        public LogisticsViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_logistics_info);
            rvImg = $(R.id.rv_img);
            tvLogisticsStatus = $(R.id.tv_logistics_status);
            tvLogisticsShippername = $(R.id.tv_logistics_shippername);
            tvLogisticsCode = $(R.id.tv_logistics_code);
            easyRvSubLogistics = $(R.id.easy_rv_sub_logistics);


        }

        @Override
        public void setData(LogisticsModel data) {
            super.setData(data);
            rvImg.setAdapter(adapter = new SeeEvaluateImgAdapter(context));
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4);
            rvImg.setLayoutManager(gridLayoutManager);
            adapter.addAll(data.getGoodsLogoList());


//            "State":"物流状态：2-在途中,3-签收,4-问题件",
            String state = data.getState();
            if (state.equals("2")) {
                state = "在途中";
            } else if (state.equals("3")) {
                state = "签收";
            } else if (state.equals("4")) {
                state = "问题件";
            }else {
                state = "无物流信息";
            }
            String srtState = "物流状态: <font color='#09c0c7'>" + state + "</font>";
            tvLogisticsStatus.setText(Html.fromHtml(srtState));
            tvLogisticsShippername.setText("承运公司: " + data.getShipperName());
            tvLogisticsCode.setText("运单编号: " + data.getLogisticCode());


            easyRvSubLogistics.setLayoutManager(new LinearLayoutManager(context));
            easyRvSubLogistics.setAdapterWithProgress(logisticsSubAdapter = new LogisticsSubAdapter(context));
            Collections.reverse(data.getTraces());
            logisticsSubAdapter.addAll(data.getTraces());
        }
    }



    class SeeEvaluateImgAdapter extends RecyclerArrayAdapter<String> {
        private Context context;

        public SeeEvaluateImgAdapter(Context context) {
            super(context);
            this.context = context;
        }

        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            return new  SeeEvaluateImgAdapter.SubImgViewHolder(parent, context);
        }

        class SubImgViewHolder extends BaseViewHolder<String> {
            private ImageView imgSub;

            public SubImgViewHolder(ViewGroup parent, Context ctx) {
                super(parent, R.layout.item_logistics_img);
                imgSub = $(R.id.img_sub);
            }

            @Override
            public void setData(String data) {
                super.setData(data);
                GlideUtils.loadImage(context, data, imgSub, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                    @Override
                    public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                    }

                    @Override
                    public void onLoadingError(String source, Exception e) {

                    }
                }, R.mipmap.default_icon, R.mipmap.default_icon);
            }
        }
    }

}
